<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
class UserController extends Controller
{
    //后台用户添加页
    public function getAdd()
    {
        //跳转添加页
        return view('admin.user.add');
        // return view('admin.index');

    }
    //用户信息添加到数据库
    public function postInsert(Request $request)
    {

        //用larvael自帶的表单验证 并使之错误信息返回
        $this->validate($request, [
            'username' => 'required|regex:/\w{6,18}/',
            'password' => 'required|regex:/\w{6,18}/',
            'repassword' => 'required|same:password',
            'email' => 'required|email',
            'phone' => 'required|regex:/1[3-8]\d{9}/',
        ],[
            'username.required' => '用户名不能为空',
            'username.regex' => '用户名6-18位',
            'password.required' => '密码不能为空',
            'password.regex' => '密码必须为6-18位',
            'repassword.required' => '确认密码不能为空',
            'repassword.same' => '两次密码不一致',
            'email.required' => '邮箱不能为空',
            'email.email' => '邮箱格式不正确',
            'phone.required' => '手机号不能为空',
            'phone.regex' => '手机号格式不正確',
        ]);
        
        //用户其他信息
        $date = $request->only(['username','password','email','phone']);
        //用户头像名称
        $date['pic'] = $this->upload($request);
        //随机一个token
        $date['token'] = str_random(50);
        //状态码 未激活为1
        $date['status'] = 1;
        //密码加密
        $date['password'] = Hash::make($date['password']);
        //用户
        $res = DB::table('user')->insert($date);
        // 判断处理结果 
        if ($res) {
               return  redirect('/admin/user/list');
        }else{
            return black();
        }
    
    }
    //处理文件上传
    public function upload(Request $request)
    {
        // dd($request->all());

        //文件上传的form表单中 必须有 enctype="multipart/form-data" 
        //检测是否有上传文件
        if($request->hasFile('pic')){
            //文件名称
            $name = md5(time()+rand(111111,8999999));
            //文件后缀名获取
            $suffix = $request->file('pic')->getClientOriginalExtension();
            $arr = ['jpg','png'];
            //判断文件上传类型
            if(!in_array($suffix,$arr)){
                echo '上传文件不符合要求';die;
            }
            //将指定文件移动到指定位置
            $request->file('pic')->move('./uploads/', $name.'.'.$suffix);
            //返回图片的位置 和名称
            return '/uploads/'.$name.'.'.$suffix;
        }
    }

    //跳转list用户列表页
        public function getList(Request $request)
        {
            //查询从表单搜索框提交的数据
            $nn = $request->input('username');
            //判断搜索框是否有值
            if($nn){
                $res = DB::table('user')->where('username','like','%'.$nn.'%')->paginate(10);
            }else{
                $res = DB::table('user')->paginate(10);
            }
            //分配变量
            return view('admin.user.list', ['res' => $res]);
       
        }
   
    //删除数据 接受ajax值 返回相应的值给ajax
        public function getDelete(Request $request)
        {   
            $id = $request->input('id');
            $res = DB::table('user')->where('id',$id)->delete();
            if($res != 0){
                echo 1;
            }
        }
    //修改用户信息
        public function getEdit(Request $request)
        {   
            //获取id
            $id = $request->input('id');
            // 查询id
            $res = DB::table('user')->where('id',$id)->first();
            //跳转到 修改显示页面
            return view('admin.user.edit', ['res' => $res]);
          
        }

        public function postUpdate(Request $request)
        {   
            //检查修改内容
            // dd($request->all());
            $this->validate($request, [
                'username' => 'required|regex:/\w{6,18}/',
                'email' => 'required|email',
                'phone' => 'required|regex:/1[3-8]\d{9}/',
            ],[
                'username.required' => '用户名不能为空',
                'username.regex' => '用户名6-18位',
                'email.required' => '邮箱不能为空',
                'email.email' => '邮箱格式不正确',
                'phone.required' => '手机号不能为空',
                'phone.regex' => '手机号格式不正確',
            ]);
            $id = $request->input('id');
            $date = $request->only('username','email','phone');
            // dd($date);
            $qq =  DB::table('user')
                    ->where('id', $id)
                    ->update($date);
            //判断修改
                    // dd($qq);
                    if($qq == 1){
                        return redirect('/admin/user/list');  
                    }else{
                         return back();
                    }

        }
      


}
